package com.atguigu.java;

import org.junit.Test;

import java.util.Arrays;
import java.util.Comparator;

import static java.util.Arrays.sort;

public class CompareTest {
    @Test
    public void test1(){
        String[] arr = {"AA", "CC", "KK", "MM", "GG", "JJ", "DD"};
        sort(arr);
        System.out.println(Arrays.toString(arr));

    }
    @Test
    public void test2(){
        Goods[] arr = new Goods[7];
        arr[0] = new Goods("lenovoMouse", 34);
        arr[1] = new Goods("dellMouse", 99);
        arr[2] = new Goods("xiaomiMouse", 55);
        arr[3] = new Goods("huaweiMouse", 94);
        arr[4] = new Goods("microsoMouse", 39);
        arr[5] = new Goods("microsoMouse", 69);
        arr[6] = new Goods("huosuoMouse", 39);
        Arrays.sort(arr);
        System.out.println(Arrays.toString(arr));

    }
    @Test
    public void test3(){
        String[] arr = new String[]{"AA","CC","KK","MM","GG","JJ","DD"};
        Arrays.sort(arr,new Comparator(){

            @Override
            public int compare(Object o1, Object o2) {
                if(o1 instanceof String && o2 instanceof String){
                    String s1=(String) o1;
                    String s2=(String) o2;
                    return -s1.compareTo(s2 );
                }
                throw new RuntimeException("输入的数据类型不一致");

            }
        });
        System.out.println(Arrays.toString(arr));

    }
    @Test
    public void test4(){
        Goods[] arr = new Goods[6];
        arr[0] = new Goods("lenovoMouse",34);
        arr[1] = new Goods("dellMouse",43);
        arr[2] = new Goods("xiaomiMouse",12);
        arr[3] = new Goods("huaweiMouse",65);
        arr[4] = new Goods("huaweiMouse",224);
        arr[5] = new Goods("microsoftMouse",43);
        Arrays.sort(arr,new Comparator(){


            @Override
            public int compare(Object o1, Object o2) {
                if (o1 instanceof Goods && o2 instanceof Goods) {
                    Goods g1=(Goods) o1;
                    Goods g2=(Goods) o2;
                    if(g1.getName().equals(g2.getName())){
                        return -Double.compare(g1.getPrice(),g2.getPrice());
                    }else{
                        return g1.getName().compareTo(g2.getName());
                    }
                }
                throw new RuntimeException("不一致");
            }
        });
        System.out.println(Arrays.toString(arr));


    }
}
